package com.xnyzc.lhy.order.service.impl.user;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xnyzc.lhy.common.penum.billRules.EIsGather;
import com.xnyzc.lhy.common.penum.billRules.EUseTimeType;
import com.xnyzc.lhy.common.penum.cms.EVehicleType;
import com.xnyzc.lhy.common.penum.order.EBillRulesName;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.ResultListUtil;
import com.xnyzc.lhy.order.entity.area.OaSysArea;
import com.xnyzc.lhy.order.entity.common.order.OrderCommon;
import com.xnyzc.lhy.order.entity.order.OaCOrder;
import com.xnyzc.lhy.mis.entity.sidebar.OaSysOffice;
import com.xnyzc.lhy.order.entity.system.OaSysBillRules;
import com.xnyzc.lhy.order.mapper.area.OaSysAreaMapper;
import com.xnyzc.lhy.order.mapper.sidebar.OaSysOfficeMapper;
import com.xnyzc.lhy.order.mapper.system.OaSysBillRulesMapper;
import com.xnyzc.lhy.order.service.user.IOaCBillRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 计费规则
 * @Author zhuyubin
 * @Date 2019-08-17 19:38
 **/
@Service
public class OaCBillRulesServiceImpl implements IOaCBillRulesService {

    @Autowired
    private OaSysBillRulesMapper oaSysBillRulesMapper;
    @Autowired
    private OrderCommon orderCommon;
    @Autowired
    private OaSysAreaMapper oaSysAreaMapper;
    @Autowired
    private OaSysOfficeMapper oaSysOfficeMapper;

    /**
     * 获取计费规则
     * @return
     */
    @Override
    public Map<String, Object> getBillRules(String orderNo){
        Map<String, Object> result = new HashMap<>(16);
        result.put("list", new ArrayList<>());
        result.put("prompt", "");
        OaCOrder oaCOrder = orderCommon.selectOn(orderNo);
        // 计费规则id
        Long rulesId = oaCOrder.getRulesId();
        if(CheckUtil.objIsEmpty(rulesId)){
            return result;
        }
        OaSysBillRules oaSysBillRules = oaSysBillRulesMapper.selectById(rulesId);
        if (CheckUtil.objIsNotEmpty(oaSysBillRules)){
            ResultListUtil resultListUtil = new ResultListUtil();
            // 公司
            Long companyId = oaSysBillRules.getCompanyId();
            if (CheckUtil.objIsNotEmpty(companyId)){
                OaSysOffice oaSysOffice = oaSysOfficeMapper.selectById(companyId);
                if (CheckUtil.objIsNotEmpty(oaSysOffice)){
                    resultListUtil.add(EBillRulesName.company.getName(), oaSysOffice.getName());
                }
            }
            // 城市
            String cityId = oaSysBillRules.getCityId();
            if (CheckUtil.objIsNotEmpty(cityId)){
                List<OaSysArea> oaSysAreas = oaSysAreaMapper.selectList(Wrappers.<OaSysArea>lambdaQuery().eq(OaSysArea::getAdCode, cityId));
                if (CheckUtil.objIsNotEmpty(oaSysAreas)){
                    OaSysArea oaSysArea = oaSysAreas.get(0);
                    resultListUtil.add(EBillRulesName.city.getName(), oaSysArea.getName());
                }
            }
            // 用车类型
            Integer vehicleType = oaSysBillRules.getVehicleType();
            if (CheckUtil.objIsNotEmpty(vehicleType)){
                resultListUtil.add(EBillRulesName.vehicleType.getName(), EVehicleType.findVehType(vehicleType).getDesc());
            }
            // 用车时间
            Integer useTimeType = oaSysBillRules.getUseTimeType();
            if (CheckUtil.objIsNotEmpty(useTimeType)){
                resultListUtil.add(EBillRulesName.useTimeType.getName(), EUseTimeType.findType(useTimeType).getTypeName());
            }
            // 驶出当前城市是否收取时间费
            Integer isGather = oaSysBillRules.getIsGather();
            if (CheckUtil.objIsNotEmpty(isGather)){
                resultListUtil.add(EBillRulesName.isGather.getName(), EIsGather.findType(isGather).getTypeName());
            }
            resultListUtil
                    .add(EBillRulesName.startFee.getName(), String.format("%s%s", oaSysBillRules.getStartFee(), EBillRulesName.yuan.getName()))
                    .add(EBillRulesName.timeFee.getName(), String.format("%s%s", oaSysBillRules.getTimeFee(), EBillRulesName.everyMinute.getName()))
                    .add(EBillRulesName.mileageFee.getName(), String.format("%s%s", oaSysBillRules.getMileageFee(), EBillRulesName.everyKilometer.getName()))
                    .add(EBillRulesName.distanceFee.getName(), String.format("%s%s", oaSysBillRules.getDistanceFee(), EBillRulesName.everyKilometer.getName()));
//                    .add(EBillRulesName.carpoolDiscountRatio.getName(), String.format("%s%s", CheckUtil.objIsNotEmpty(oaSysBillRules.getCarpoolDiscountRatio()) ? oaSysBillRules.getCarpoolDiscountRatio() : "0.00", EBillRulesName.ratio.getName()));
            result.put("list", resultListUtil.getList());
            result.put("prompt", String.format(EBillRulesName.distanceKm.getName(), this.numIsNull(oaSysBillRules.getDistanceKm())));
        }
        return result;
    }

    /**
     * 对数字的空处理
     * @param value
     * @return
     */
    private String numIsNull(Object value){
        return CheckUtil.objIsEmpty(value) ? "0" : value.toString();
    }
}
